import 'package:boss/common/common.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/ui/page/offersManagement/page/offersDetail/offers_detail_page.dart';
import 'package:boss/ui/widget/loader/empty_widget.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:provider/provider.dart';

import 'offers_item.dart';

class OffersList extends StatefulWidget {
  final int status;

  const OffersList({Key key, @required this.status}) : super(key: key);

  @override
  _OffersListState createState() => _OffersListState();
}

class _OffersListState extends State<OffersList> with AutomaticKeepAliveClientMixin {
  Future<Result> _loadTask() async {
    var offersProvider = Provider.of<OffersProvider>(
      context,
      listen: false,
    );
    var flag = await offersProvider.initData(widget.status);
    return flag ? Result.value(null) : Result.error(Constant.httpErrText);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Loader(
      loadTask: _loadTask,
      builder: (context) {
        return Consumer<OffersProvider>(
          builder: (context, offersProvider, child) {
            var offers = offersProvider.offers[widget.status];

            if (offers.length == 0) {
              return EmptyWidget();
            }

            return ListView.builder(
              padding: EdgeInsets.symmetric(
                horizontal: 30.w,
                vertical: 30.w,
              ),
              itemBuilder: (context, index) {
                var current = offers[index];

                return OffersItem(
                  data: current,
                  onTap: () {
                    Navigator.pushNamed(context, OffersDetailPage.pageName, arguments: {
                      'data': current,
                      'type': widget.status,
                    });
                  },
                );
              },
              itemCount: offers.length,
            );
          },
        );
      },
    );
  }

  @override
  bool get wantKeepAlive => true;
}
